<%@page contentType="text/html;charset=utf-8"%>
<%@include file="../../core/common.jsp"%>
<html>
<head>
<title></title>
<script src="${CTX}/static/plugins/ukey/Syunew3.js"></script>
</head>
<script>
	function read_() {
		var self_ = $(this);
		var devicePath, id1, id2;
		$.mask();
		var uk = new SoftKey3W();
		uk.Socket_UK.onopen = function(e) {
			uk.ResetOrder();
		};
		uk.Socket_UK.onerror = function(e) {
			$.error('请下载并安装ukey驱动');
		};
		uk.Socket_UK.onclose = function(e) {
			$.unmask();
		};
		uk.Socket_UK.onmessage = function(e) {
			var ukData = JSON.parse(e.data);
			if (ukData.type != 'Process') {
				return;
			}
			switch (ukData.order) {
			case 0:
				uk.FindPort(0);
				break;
			case 1:
				if (ukData.LastError != 0) {
					$.error('未发现ukey，请插入ukey');
					uk.Socket_UK.close();
					return;
				}
				devicePath = ukData.return_value;
				uk.GetID_1(devicePath);
				break;
			case 2:
				if (ukData.LastError != 0) {
					$.error('读取ukey错误，错误码为：' + ukData.LastError.toString());
					uk.Socket_UK.close();
					return;
				}
				id1 = ukData.return_value;
				uk.GetID_2(devicePath);
				break;
			case 3:
				if (ukData.LastError != 0) {
					$.error('读取ukey错误，错误码为：' + ukData.LastError.toString());
					uk.Socket_UK.close();
					return;
				}
				id2 = ukData.return_value;
				uk.Socket_UK.close();
				self_.textbox('setValue', id1.toString(16).toUpperCase() + id2.toString(16).toUpperCase());
				break;
			}
		};
	}
	function get_() {
		var self_ = $(this);
		$.prompt('请输入用户帐号', function(value) {
			$.mask();
			var sysUser = $.loadJSON('${CTX}/mgt/sys/user/get/' + value);
			if (sysUser) {
				$('#userId').val(sysUser.id);
				self_.textbox('setValue', sysUser.name);
			} else {
				$.error('该帐号不存在');
			}
			$.unmask();
		});
	}
</script>
<body>
	<div class="easyui-panel" data-options="fit:true,border:false,footer:'#fb'">
		<form id="fm">
			<input id="userId" name="userId" type="hidden">
			<table class="form-layout w70 p-t-10 p-r-30">
				<tr>
					<td>ukey：</td>
					<td><input name="id" class="easyui-textbox" data-options="required:true,editable:false,buttonIcon:'icon-add',onClickButton:read_" /></td>
				</tr>
				<tr>
					<td>用户：</td>
					<td><input class="easyui-textbox" data-options="required:true,editable:false,buttonIcon:'icon-search',onClickButton:get_" /></td>
				</tr>
			</table>
		</form>
	</div>
	<div id="fb">
		<a id="save" class="form-btn success">确定</a>
	</div>
</body>
<script>
	$(function() {
		$('#save').click(function() {
			if (!$('#fm').form('validate')) {
				return;
			}
			var devicePath;
			$.mask();
			var uk = new SoftKey3W();
			uk.Socket_UK.onopen = function(e) {
				uk.ResetOrder();
			};
			uk.Socket_UK.onerror = function(e) {
				$.error('请下载并安装ukey驱动');
			};
			uk.Socket_UK.onclose = function(e) {
				$.unmask();
			};
			uk.Socket_UK.onmessage = function(e) {
				var ukData = JSON.parse(e.data);
				if (ukData.type != 'Process') {
					return;
				}
				switch (ukData.order) {
				case 0:
					uk.FindPort(0);
					break;
				case 1:
					if (ukData.LastError != 0) {
						$.error('未发现ukey，请插入ukey');
						uk.Socket_UK.close();
						return;
					}
					devicePath = ukData.return_value;
					uk.SetCal_2($('#userId').val(), devicePath);
					break;
				case 2:
					if (ukData.LastError != 0) {
						$.error('设置ukey密钥错误，错误码为：' + ukData.LastError.toString());
						uk.Socket_UK.close();
						return;
					}
					$.ajax({
						url : '${CTX}/mgt/sys/ukey/bind',
						data : $('#fm').serialize(),
						success : function(data) {
							uk.Socket_UK.close();
							$.handle(data, function() {
								$.close();
							});
						}
					});
					break;
				}
			}
		});
	});
</script>
</html>